package edu.nf.meituan.dao.shop.impl;

import edu.nf.meituan.dao.shop.ShopDAO;
import edu.nf.meituan.model.shop.Shop;
import edu.nf.util.DBUtils;
import org.nf.db.util.SqlExecutor;
import org.nf.db.util.result.BeanHandler;
import org.nf.db.util.result.BeanListHandler;

import java.util.List;

/**
 * 商家 dao 接口的实现类
 */
public class ShopDAOImpl implements ShopDAO {
    /**
     * 返回全部店铺
     *
     * @return
     */
    @Override
    public List<Shop> listShop() {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        BeanListHandler<Shop> listHandler = new BeanListHandler<>(Shop.class);
        String sql = "select shop_id,shop_img,shop_types_id,u_id, shop_name,shop_phone,shop_address,shop_created_at from shop";
        return sqlExecutor.executeQuery(sql, listHandler);
    }

    /**
     * 用户入住商家
     * @param typeId
     * @param uId
     * @param shopName
     * @param shopPhone
     * @param shopAddress
     * @return
     */
    @Override
    public int registerShop(int typeId, int uId, String shopName, String shopPhone, String shopAddress) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());

        String sql = "insert into shop(shop_types_id,u_id,shop_name,shop_phone,shop_address) values(?,?,?,?,?)";

        return sqlExecutor.executeUpdate(sql, typeId, uId, shopName, shopPhone, shopAddress);
    }

    /**
     * 查询手机号是否注册
     * @param phone
     * @return
     */
    @Override
    public Shop equeryPhone(String phone) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());

        String sql ="select shop_phone from shop where shop_phone = ?";

        BeanHandler<Shop> shopBeanHandler = new BeanHandler<>(Shop.class);

        return sqlExecutor.executeQuery(sql,shopBeanHandler,phone);
    }

    /**
     * 通过名称查找全部店铺
     * @param shopName
     * @return
     */
    @Override
    public List<Shop> listShopByShopName(String shopName) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        BeanListHandler<Shop> listHandler = new BeanListHandler<>(Shop.class);
        String sql = "SELECT shop_id,shop_img,shop_types_id, u_id,shop_name,shop_phone,shop_address, shop_created_at FROM shop WHERE shop_name LIKE '%?%'";
        return sqlExecutor.executeQuery(sql,listHandler,shopName);
    }

    /**
     * 通过名称查询全部
     * @param shopName
     * @return
     */
    @Override
    public List<Shop> listShopByName(String shopName) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        BeanListHandler<Shop> listHandler = new BeanListHandler<>(Shop.class);
        String sql = "SELECT shop_id,shop_img,shop_types_id, u_id,shop_name,shop_phone,shop_address, shop_created_at FROM shop WHERE shop_name LIKE '%?%'";
        return sqlExecutor.executeQuery(sql,listHandler,shopName);
    }
}
